<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="ntModalLabel">{:lang('add/change_town')}</h4>
</div>
<form class="modal-body formbox">
        <div class="row clearfix">
                <div class="col-md-12 column">
                    <div class="alert alert-dismissable alert-info">
                        <button type="button" class="close">×</button>
                         <p></p>
                    </div>
                </div>
            </div>
    <table class="table table-striped table-hover ntTable">
        <tbody>
        <tr>
            <td>{:lang('country')}<span class="required"></span></td>
            <td>
                <select class="form-control col-md-2" id="changeselect" name="country_id">
                    <option value=''>{:lang('please_select')}</option>
                    {volist name="country" id="vo"}
                    {eq name="vo.id" value="$info.country_id|default=''"}
                    <option value="{$vo.id}" selected>{$vo.name}</option>
                    {else/}
                    <option value="{$vo.id}">{$vo.name}</option>
                    {/eq}
                    {/volist}
                </select>
            </td>
        </tr>
        <tr>
            <td>{:lang('state')}<span class="required"></span></td>
            <td>
                <select class="form-control" id="changecity" name="province_id">
                </select>
            </td>
        </tr>
        <tr>
            <td>{:lang('city')}<span class="required"></span></td>
            <td>
                <select class="form-control" id="listpro" name="city_id">
                </select>
            </td>
        </tr>
        <tr>
            <td>{:lang('town_name')}<span class="required"></span></td>
            <td>
                <input type="text" class="form-control" name="name" placeholder="{:lang('town_name')}" value="{$info.name|default=''}">
            </td>
        </tr>
        </tbody>
    </table>
    <input type="hidden" id="act" name="act" value="{$str}">
    <input type="hidden" name="id" id="id" value="{$info.id|default=''}">
    <div style="display: none">
        <span id="textcoun">{$info.country_id|default=''}</span>
        <span id="textpro">{$info.province_id|default=''}</span>
        <span id="textcity">{$info.city_id|default=''}</span>
    </div>
</form>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span>{:lang('cancel')}
    </button>
    <button type="button" id="quedin" class="btn btn-primary" style="padding-left:24px;padding-right:24px">
        <span class="glyphicon glyphicon-floppy-save"></span>{:lang('save_and_exit')}
    </button>
    {if ($str eq 'townadd')}
    <button type="button" id="qudins" class="btn btn-primary" style="padding-left:24px;padding-right:24px">
        <span class="glyphicon glyphicon-floppy-disk"></span>{:lang('save_and_continue_adding')}
    </button>
    {/if}
</div>
<script>
    var country_id = $('#textcoun').text();//
    var textpro = $('#textpro').text();//省份id
    var textcity = $('#textcity').text();//城市id
    if ($('#act').val() == 'townedit') {
        getproes(country_id, 'provinces', 1);//获取这个编辑的城市
        getproes(textpro, 'city', 2);
    }
    //数据的提交,保存病退出
    $('#quedin').click(function () {
        subfn(1)
    })
    //保存并继续添加
    $('#qudins').click(function () {
        subfn(2)
    })
    var addbm = true;
    function subfn(code) {
        if (addbm) {
            $.ajax({
                url: '/Admin/prefecture/' + $('#act').val(),
                type: 'post',
                data: $(".formbox").serialize(),
                dataType: 'json',
                success: function (res) {
                    if (res.status != 1) {
                        change_mes_type('.modal-body .alert',res.status)
                        $('.modal-body .alert').show()
                        $('.modal-body .alert p').html('*'+res.msg+'<br>')
                        //toastr.warning(res.msg)
                    } else {
                        change_mes_type('.alert',res.status)
                        if (code == 1) {
                            addbm = false
                            //toastr.info(res.msg)
                            $('.alert').show()
                            $('.alert p').html('*'+res.msg+'<br>')
                            $('#town_editModal').modal('hide')
                            /* setTimeout(function () {
                                $('#town_editModal').modal('toggle')
                            }, 1000) */
                        } else {
                            $('.alert').show()
                            $('.alert p').html('*'+res.msg+'<br>')
                            //toastr.info(res.msg)
                        }

                    }
                }
            });
        }
    }
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            $('form').each(function () {
                event.preventDefault();
            });
            subfn(1)
        }
    });
    $('#changeselect').change(function () {
        //获取国家的下的省份
        var coun_id = $('#changeselect').val();
        getproes(coun_id, 'provinces', 1);
    });
    $('#changecity').change(function () {
        //获取省份的城市
        var pro_id = $('#changecity').val();
        getproes(pro_id, 'city', 2);
    })
    function getproes(coun_id, table, code) {
        $.ajax({
            url: '/admin/api/getpro',
            type: 'post',
            data: {
                table: table,
                pro_id: coun_id
            },
            dataType: 'json',
            success: function (res) {
                model(res, code);
            }
        });
    }
    function model(res, code) {
        if (code == 1) {
            if (res.length) {
                var html = "<option value=''>"+langcon.please_select+"</option>"
                for (var i = 0; i < res.length; i++) {
                    var id = res[i].id;
                    if (id == textpro) {
                        html += '<option selected value="' + res[i].id + '">' + res[i].name + '</option>';
                    } else {
                        html += '<option value="' + res[i].id + '">' + res[i].name + '</option>';
                    }
                }
                $('#changecity').html(html);
            }
        } else {
            if (res.length) {
                var html = "<option value=''>"+langcon.please_select+"</option>"
                for (var i = 0; i < res.length; i++) {
                    var id = res[i].id;
                    if (id == textcity) {
                        html += '<option selected value="' + res[i].id + '">' + res[i].name + '</option>';
                    } else {
                        html += '<option value="' + res[i].id + '">' + res[i].name + '</option>';
                    }
                }
                $('#listpro').html(html);
            }
        }
    }
</script>